package com.thrsdk.utils;


import android.content.Context;
import android.text.TextUtils;

import com.thrsdk.DYFusionSDK;
import com.thrsdk.THFSDKClient;
import com.thrsdk.common.network.request.HttpRequestClient;
import com.thrsdk.mylibrary.BuildConfig;
import com.thrsdk.utils.local.SpHelper;
import com.thrsdk.utils.local.bean.BackupBean;
import com.thrsdk.utils.local.bean.ConfigBean;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * 网络请求地址处理 地址切换等
 */
public class DomainHelper {
    private static DomainHelper instance;
    private List<BackupBean> mDomainList;  //全部网络请求对象的地址
    private BackupBean mCurrentDomainBean; //当前网络请求的对象
    private int mCurrentPosition; //当前对象在list中的位置
    private List<String> mReqDomainUrlList = new ArrayList<>(); //请求备份域名地址的地址

    private DomainHelper() {
        BackupBean localBackBean = SpHelper.getCurrentBackupBean();
        if (localBackBean == null) {
            localBackBean = new BackupBean();
            localBackBean.setDomain(GameCofig.getUrl(THFSDKClient.getInstance().getContext()));

        }
        mCurrentDomainBean = localBackBean;
        mCurrentPosition = 0;

        if (!BuildConfig.DEBUG) {//生产环境
            mReqDomainUrlList.add("https://domain.dogamedata.com/statics/domain/" + BuildConfig.APP_ID + ".json");
            mReqDomainUrlList.add("https://domain.dongyou2.com/statics/domain/" + BuildConfig.APP_ID + ".json");
            mReqDomainUrlList.add("https://api.58wan.cn/statics/domain/" + BuildConfig.APP_ID + ".json");
        } else {
            mReqDomainUrlList.add("https://domain.dogamedata.com/statics/dev-domain/" + BuildConfig.TEST_APP_ID + ".json");//宁德动游主体
            mReqDomainUrlList.add("https://domain.dongyou2.com/statics/dev-domain/" + BuildConfig.TEST_APP_ID + ".json");//宁德动游主体
            mReqDomainUrlList.add("https://api.58wan.cn/statics/dev-domain/" + BuildConfig.TEST_APP_ID + ".json");//世纪德安主体
        }
    }

    public static DomainHelper getInstance() {
        if (instance == null) {
            synchronized (DomainHelper.class) {
                if (instance == null) {
                    instance = new DomainHelper();
                }
            }
        }
        return instance;
    }

    /**
     * 获取备份的域名列表
     */
    public void getDomainListFromNet(Context context, int position) {
        String url = "";
        if (position < mReqDomainUrlList.size()) {
            url = mReqDomainUrlList.get(position);
        }
        if (TextUtils.isEmpty(url)) {
            setDomainList(null, false);
            return;
        }
        TreeMap<String, String> reqMap2 = new TreeMap<>();
        HttpRequestClient.sendPostRequest(url, reqMap2, ConfigBean.class, new HttpRequestClient.ResultHandler<ConfigBean>(context) {
            @Override
            public void onSuccess(ConfigBean configBean, boolean result, String msg) {
                if (null != configBean) {
                    List<BackupBean> domains = configBean.data.backups;
                    setDomainList(domains, true);
                } else {
                    setDomainList(null, false);

                }

            }

            @Override
            public void onFailure(Throwable t) {
                getDomainListFromNet(context, position + 1);
            }
        });
    }


    private void setDomainList(List<BackupBean> backupBeanList, boolean isSuccess) {
        if (backupBeanList == null) {
            backupBeanList = new ArrayList<>();
        }
        if (isSuccess) { //网络请求备份接口成功
            mDomainList = backupBeanList;
            SpHelper.setBackupBeanList(backupBeanList);
        } else {
            mDomainList = SpHelper.getBackupBeanList();
        }
        if (mDomainList != null) {
            mDomainList.add(0, mCurrentDomainBean);
        }
    }

    /**
     * 切换域名
     */
    public void changeDomain(String oldUrl) {
        if (!TextUtils.isEmpty(oldUrl)) {
            //如果当前的domain与上一次的网络请求不一样的情况下才能切换网络
            if (oldUrl.contains(getDomainMain()) && mDomainList != null && mDomainList.size() > 0) {
                mCurrentPosition++;
                if (mDomainList.size() <= mCurrentPosition) {
                    mCurrentPosition = 0;
                }
                toChange();
            }
        }
    }

    private void toChange() {
        if (mDomainList.get(mCurrentPosition) != null) {
            mCurrentDomainBean = mDomainList.get(mCurrentPosition);
            SpHelper.setCurrentBackupBean(mCurrentDomainBean);
        }
    }

    /**
     * 获取住域名
     */
    public String getDomainMain() {
        if (mCurrentDomainBean != null) {
            return mCurrentDomainBean.getDomain();
        } else {
            return "";
        }
    }


}
